package cn.tedu.rms.cart.service.impl;

import cn.tedu.rms.cart.mapper.CartMapper;
import cn.tedu.rms.cart.pojo.dto.CartDeleteParam;
import cn.tedu.rms.cart.pojo.dto.CartQuery;
import cn.tedu.rms.cart.pojo.dto.CartSaveParam;
import cn.tedu.rms.cart.pojo.entity.Cart;
import cn.tedu.rms.cart.pojo.vo.CartVO;
import cn.tedu.rms.cart.service.CartService;
import cn.tedu.rms.dish.mapper.DishMapper;
import cn.tedu.rms.dish.pojo.dto.DishQuery;
import cn.tedu.rms.dish.pojo.vo.DishVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public void insertCart(CartSaveParam cartSaveParam) {
        CartQuery cartQuery = new CartQuery();
        BeanUtils.copyProperties(cartSaveParam,cartQuery);
        List<CartVO> cartVOS = cartMapper.select(cartQuery);
        if(cartVOS!=null&&!cartVOS.isEmpty()){
            CartVO cartVO = cartVOS.get(0);
            Integer numOld = Integer.valueOf(cartVO.getNumber());
            Integer numInsert = Integer.valueOf(cartSaveParam.getNumber());
            Integer numNew = numOld+numInsert;
            cartSaveParam.setNumber(numNew.toString());
            log.debug("加入相同的菜品，修改购物车中菜品数量业务,参数：{}",cartSaveParam);
            cartMapper.update(cartSaveParam);

        }else{
            log.debug("加入购物车业务,参数：{}",cartSaveParam);
            Cart cart = new Cart();
            BeanUtils.copyProperties(cartSaveParam,cart);
            cart.setCreateTime(new Date());
            cartMapper.insert(cart);
        }


    }

    @Override
    public void deleteCartById(Long id) {
        log.debug("删除购物车中记录业务,参数：{}",id);
        cartMapper.deleteById(id);
    }

    @Override
    public void deleteCartByIds(CartDeleteParam cartDeleteParam) {
        log.debug("批量删除购物车中记录业务,参数：{}",cartDeleteParam);
        cartMapper.deleteByIds(cartDeleteParam.getIds());
    }

    @Override
    public List<CartVO> selectCart(CartQuery cartQuery) {
        log.debug("查询购物车记录业务,参数：{}",cartQuery);
        List<CartVO> cartVOList = cartMapper.select(cartQuery);
        for(CartVO cartVO : cartVOList){
            supplementCartVO(cartVO);
        }
        log.debug("查询的购物车记录cartVOList:{}",cartVOList);
        return cartVOList;
    }

    /**
     * 补充cartVO里的内容
     * @param cartVO
     */
    public void supplementCartVO(CartVO cartVO){
        DishQuery dishQuery = new DishQuery();
        dishQuery.setId(cartVO.getDishId());
        List<DishVo> dishVos = dishMapper.selectDish(dishQuery);
        DishVo dishVo = dishVos.get(0);
        cartVO.setDishName(dishVo.getName());//菜名
        cartVO.setPrice(dishVo.getPrice());//价格
        cartVO.setTotalPrice(dishVo.getPrice()*Integer.valueOf(cartVO.getNumber()));//每一项总价格
        cartVO.setImgUrl(dishVo.getImgUrl());//菜品图片地址
    }
}
